<template>
  <div class="page">
    <div class="tree">
      <el-tree check-strictly
               :load="loadChildren"
               show-checkbox
               highlight-current
               lazy
               node-key="id"
               :props="defaultProps"
               :filter-node-method="filterNode"
               ref="tree">
      </el-tree>
      <div class="groupMemberManage_sub_button" v-if="isShowTree">
        <el-button type="warning" @click="cancelTree">取消勾选</el-button>
        <el-button type="primary" @click="getTreeNodes">添加成员</el-button>
      </div>
    </div>

  </div>
</template>

<script>
  import treeApi from '@/api/treeApi'

  export default {
    data () {
      return {
        defaultProps: {
          label: 'name',
          isLeaf:'isLeaf'
        },
        isShowTree:true,
      }
    },
    methods: {
      /*   handleCheckChange(data, checked, indeterminate) {
           console.log(data, checked, indeterminate);
         },*/
      /*     handleNodeClick(data) {
             console.log(data);
           },*/
      loadChildren: function (node, resolve) {
        var that =this;
        try {
          if (node.data.type === 2) {
            return resolve([]);
          }
        } catch (e) {

        }
        if(node.data){
          that.getChild(node.data.id , node.data.type ,resolve);
        }else{
          setTimeout(function(){ that.getChild( 67 , 0 ,resolve);}, 0);
        }

      },
      loadNode (node, resolve) {
        console.log(node)
        if (node.level === 0) {
          return resolve([])
        }
        if (node.level > 1) return resolve([])
        setTimeout(() => {
          var jsonData={
            initID: 67,
            managePriv: 7,
            parentId: 67,
            type: 1,
            userId: "wxq"
          };
          treeApi.getTalk(jsonData)
            .then(function (res) {
              console.log(res)
              if(res.success){
                var childNodes = [];
                if (res.obj) {
                  childNodes = res.obj;
                  childNodes.forEach(function (value,index) {
                    childNodes[index].isLeaf=value.type===1;
                  });
                }
                resolve(childNodes);
              }
            })
            .catch(function (error) {
              console.log(error);
            });
        }, 500)
      },
      getChild: function (id, type,resolve) {
                /*
          * initID: 67
        managePriv: 7
        parentId: 158
        type: 1
        userId: "wxq"*/
        var jsonData={
          initID: 67,
          managePriv: 7,
          parentId: 67,
          type: 1,
          userId: "wxq"
        };
        treeApi.getTalk(jsonData)
          .then(function (res) {
            if(res.success){
              var childNodes = [];
              if (res.obj) {
                childNodes = res.obj;
                childNodes.forEach(function (value,index) {
                  childNodes[index].isLeaf=value.type===2;
                });
              }
              resolve(childNodes);
            }
          })
          .catch(function (error) {
            console.log(error);
          });

      },
      filterNode:function(value, data) {
        if (!value) return true;
        return data.name.indexOf(value) !== -1;
      },
      //取消树
      cancelTree:function(){
        // this.isShowTree = false;
        this.$refs.tree.setCheckedKeys([]);
      },
      //添加成员
      getTreeNodes(){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

<style lang="scss" scoped>
.tree{
  height: 20px;
}
</style>
